package com.guaju.yueshangLife.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;
import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.activity.mine.AdviceActivity;
import com.guaju.yueshangLife.base.MyAppcomActivity;
import com.guaju.yueshangLife.dao.SearchHistoryModel;
import com.guaju.yueshangLife.databinding.ActivitySearchBinding;
import com.guaju.yueshangLife.databinding.DialogCommonBinding;
import com.guaju.yueshangLife.global.BaseApplication;
import com.guaju.yueshangLife.viewmodel.SearchHistoryMV;

import java.util.ArrayList;

import xyz.bboylin.universialtoast.UniversalToast;

public class SearchActivity extends MyAppcomActivity {

    private ActivitySearchBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_search);
        //一体栏
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.home_background));
        //TODO 给回车框添加点击事件，并且将搜索内容存储到数据库
        initData();
        setListener();


    }

    private void setListener() {
        TextView cancle = binding.searchbar.getCancle();
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        clearSearchHistory();
    }

    private void initData() {
        //读取数据库中存储的搜索记录
        ArrayList<SearchHistoryModel> historys = BaseApplication.getLiteOrm().query(SearchHistoryModel.class);
        SearchHistoryMV searchHistoryMV = new SearchHistoryMV(this, historys);
        binding.setViewmodel(searchHistoryMV);

    }

    public void clearSearchHistory() {
        binding.flClear.setOnClickListener(new View.OnClickListener() {

            private AlertDialog alertDialog;

            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                DialogCommonBinding dialogCommonBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.dialog_common, null, false);
                dialogCommonBinding.dialogConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        binding.flowlayout.removeAllViews();
                        BaseApplication.getLiteOrm().delete(SearchHistoryModel.class);
                        alertDialog.hide();
                        UniversalToast.makeText(SearchActivity.this, "清除记录成功", UniversalToast.LENGTH_SHORT, UniversalToast.EMPHASIZE)
                                .setGravity(Gravity.CENTER, 0, 0)
                                .setBackground(getResources().getDrawable(R.drawable.shape_toast_bg)) // 设置背景
                                .setLeftIconRes(R.drawable.sucess_toast)
                                .show();

                    }
                });
                dialogCommonBinding.dialogCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.hide();
                    }
                });
                builder.setView(dialogCommonBinding.getRoot());
                alertDialog = builder.create();
                alertDialog.show();
            }
        });

    }


}
